package com.stedu.service.impl;

import com.stedu.bean.CustomUser;
import com.stedu.bean.SysUser;
import com.stedu.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.TreeSet;

@Service
public class UserDetailsServiceImpl implements UserDetailsService {
    @Autowired
    private SysUserService sysUserService;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        SysUser byUser = sysUserService.getByUsername(username);
        //byUser -> UserDetails接口实现类的对象
        if (byUser == null) {
            throw  new UsernameNotFoundException("用户名和密码错误");
        }
        CustomUser user = new CustomUser(byUser, new TreeSet<>());
        return user;
    }
}
